/*     */ package com.zimbra.cs.service.admin;
/*     */ 
/*     */ import com.zimbra.common.account.Key.DistributionListBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AdminConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.cs.account.AccessManager.AttrRightChecker;
/*     */ import com.zimbra.cs.account.AttributeClass;
/*     */ import com.zimbra.cs.account.DistributionList;
/*     */ import com.zimbra.cs.account.DynamicGroup;
/*     */ import com.zimbra.cs.account.Entry;
/*     */ import com.zimbra.cs.account.Group;
/*     */ import com.zimbra.cs.account.Group.GroupOwner;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.accesscontrol.AdminRight;
/*     */ import com.zimbra.cs.account.accesscontrol.GranteeType;
/*     */ import com.zimbra.cs.account.accesscontrol.Rights.Admin;
/*     */ import com.zimbra.cs.account.accesscontrol.TargetType;
/*     */ import com.zimbra.soap.JaxbUtil;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import com.zimbra.soap.admin.message.GetDistributionListRequest;
/*     */ import com.zimbra.soap.admin.type.DistributionListSelector;
/*     */ import com.zimbra.soap.admin.type.DistributionListSelector.DistributionListBy;
/*     */ import java.util.Arrays;
/*     */ import java.util.Collections;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class GetDistributionList
/*     */   extends DistributionListDocumentHandler
/*     */ {
/*     */   public boolean domainAuthSufficient(Map context)
/*     */   {
/*  55 */     return true;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public boolean defendsAgainstDelegateAdminAccountHarvesting()
/*     */   {
/*  63 */     return true;
/*     */   }
/*     */   
/*     */   protected Group getGroup(Element request) throws ServiceException
/*     */   {
/*  68 */     Element eDL = request.getElement("dl");
/*  69 */     String key = eDL.getAttribute("by");
/*  70 */     String value = eDL.getText();
/*     */     
/*  72 */     return Provisioning.getInstance().getGroup(Key.DistributionListBy.fromString(key), value);
/*     */   }
/*     */   
/*  75 */   private static String[] minimumAttrs = { "zimbraId", "zimbraMailAlias" };
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  80 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  81 */     GetDistributionListRequest req = (GetDistributionListRequest)JaxbUtil.elementToJaxb(request);
/*     */     
/*  83 */     int limit = req.getLimit() == null ? 0 : req.getLimit().intValue();
/*  84 */     if (limit < 0) {
/*  85 */       throw ServiceException.INVALID_REQUEST("limit" + limit + " is negative", null);
/*     */     }
/*  87 */     int offset = req.getOffset() == null ? 0 : req.getOffset().intValue();
/*  88 */     if (offset < 0) {
/*  89 */       throw ServiceException.INVALID_REQUEST("offset" + offset + " is negative", null);
/*     */     }
/*  91 */     boolean sortAscending = !Boolean.FALSE.equals(req.isSortAscending());
/*  92 */     Set<String> reqAttrs = getReqAttrs(req.getAttrs(), AttributeClass.distributionList);
/*  93 */     DistributionListSelector dlSel = req.getDl();
/*  94 */     Key.DistributionListBy dlBy = dlSel.getBy().toKeyDistributionListBy();
/*  95 */     AccessManager.AttrRightChecker arc = null;
/*  96 */     Group group = getGroupFromContext(context);
/*  97 */     if (group == null) {
/*  98 */       if (Key.DistributionListBy.name.equals(dlBy)) {
/*  99 */         Entry pseudoTarget = pseudoTargetInSameDomainAsEmail(TargetType.dl, dlSel.getKey());
/* 100 */         if (null != pseudoTarget) {
/* 101 */           AdminAccessControl aac = checkDistributionListRight(zsc, (DistributionList)pseudoTarget, AdminRight.PR_ALWAYS_ALLOW);
/*     */           
/* 103 */           arc = aac.getAttrRightChecker(pseudoTarget);
/*     */         }
/*     */       }
/* 106 */       if (arc != null) {
/* 107 */         defendAgainstGroupHarvestingWhenAbsent(dlBy, dlSel.getKey(), zsc, new AdminDocumentHandler.GroupHarvestingCheckerUsingGetAttrsPerms(this, zsc, arc, Arrays.asList(minimumAttrs)));
/*     */       }
/*     */       else {
/* 110 */         defendAgainstGroupHarvestingWhenAbsent(dlBy, dlSel.getKey(), zsc, Rights.Admin.R_getDistributionList);
/*     */       }
/* 112 */     } else if (group.isDynamic()) {
/* 113 */       AdminAccessControl aac = checkDynamicGroupRight(zsc, (DynamicGroup)group, AdminRight.PR_ALWAYS_ALLOW);
/* 114 */       arc = aac.getAttrRightChecker(group);
/*     */     } else {
/* 116 */       AdminAccessControl aac = checkDistributionListRight(zsc, (DistributionList)group, AdminRight.PR_ALWAYS_ALLOW);
/*     */       
/* 118 */       arc = aac.getAttrRightChecker(group);
/*     */     }
/* 120 */     defendAgainstGroupHarvesting(group, dlBy, dlSel.getKey(), zsc, new AdminDocumentHandler.GroupHarvestingCheckerUsingGetAttrsPerms(this, zsc, arc, Arrays.asList(minimumAttrs)));
/*     */     
/*     */ 
/* 123 */     Element response = zsc.createElement(AdminConstants.GET_DISTRIBUTION_LIST_RESPONSE);
/* 124 */     Element eDL = encodeDistributionList(response, group, true, false, reqAttrs, arc);
/*     */     
/*     */ 
/* 127 */     boolean allowMembers = true;
/* 128 */     if (group.isDynamic()) {
/* 129 */       allowMembers = arc == null ? true : arc.allowAttr("member");
/*     */     } else {
/* 131 */       allowMembers = arc == null ? true : arc.allowAttr("zimbraMailForwardingAddress");
/*     */     }
/*     */     
/* 134 */     if (allowMembers) {
/* 135 */       encodeMembers(response, eDL, group, offset, limit, sortAscending);
/*     */     }
/* 137 */     return response;
/*     */   }
/*     */   
/*     */   private void encodeMembers(Element response, Element dlElement, Group group, int offset, int limit, boolean sortAscending) throws ServiceException {
/*     */     String[] members;
/*     */     String[] members;
/* 143 */     if ((group instanceof DynamicGroup)) {
/* 144 */       members = ((DynamicGroup)group).getAllMembers(true);
/*     */     } else {
/* 146 */       members = group.getAllMembers();
/*     */     }
/*     */     
/* 149 */     if ((offset > 0) && (offset >= members.length)) {
/* 150 */       throw ServiceException.INVALID_REQUEST("offset " + offset + " greater than size " + members.length, null);
/*     */     }
/*     */     
/* 153 */     int stop = offset + limit;
/* 154 */     if (limit == 0) {
/* 155 */       stop = members.length;
/*     */     }
/* 157 */     if (stop > members.length) {
/* 158 */       stop = members.length;
/*     */     }
/*     */     
/* 161 */     if (sortAscending) {
/* 162 */       Arrays.sort(members);
/*     */     } else {
/* 164 */       Arrays.sort(members, Collections.reverseOrder());
/*     */     }
/* 166 */     for (int i = offset; i < stop; i++) {
/* 167 */       dlElement.addElement("dlm").setText(members[i]);
/*     */     }
/*     */     
/* 170 */     response.addAttribute("more", stop < members.length);
/* 171 */     response.addAttribute("total", members.length);
/*     */   }
/*     */   
/*     */   public static Element encodeDistributionList(Element e, Group group) throws ServiceException
/*     */   {
/* 176 */     return encodeDistributionList(e, group, true, false, null, null);
/*     */   }
/*     */   
/*     */   public static Element encodeDistributionList(Element e, Group group, boolean hideMembers, boolean hideOwners, Set<String> reqAttrs, AccessManager.AttrRightChecker attrRightChecker)
/*     */     throws ServiceException
/*     */   {
/* 182 */     return encodeDistributionList(e, group, hideMembers, hideOwners, true, reqAttrs, attrRightChecker);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public static Element encodeDistributionList(Element e, Group group, boolean hideMembers, boolean hideOwners, boolean encodeAttrs, Set<String> reqAttrs, AccessManager.AttrRightChecker attrRightChecker)
/*     */     throws ServiceException
/*     */   {
/* 190 */     Element eDL = e.addElement("dl");
/* 191 */     eDL.addAttribute("name", group.getUnicodeName());
/* 192 */     eDL.addAttribute("id", group.getId());
/* 193 */     eDL.addAttribute("dynamic", group.isDynamic());
/*     */     
/* 195 */     if (encodeAttrs) {
/* 196 */       Set<String> hideAttrs = null;
/* 197 */       if (hideMembers) {
/* 198 */         hideAttrs = new HashSet();
/* 199 */         if (group.isDynamic()) {
/* 200 */           hideAttrs.add("member");
/*     */         } else {
/* 202 */           hideAttrs.add("zimbraMailForwardingAddress");
/*     */         }
/*     */       }
/*     */       
/* 206 */       ToXML.encodeAttrs(eDL, group.getUnicodeAttrs(), "n", reqAttrs, hideAttrs, attrRightChecker);
/*     */     }
/*     */     
/*     */ 
/* 210 */     if (!hideOwners) {
/* 211 */       encodeOwners(eDL, group);
/*     */     }
/*     */     
/* 214 */     return eDL;
/*     */   }
/*     */   
/*     */   public static Element encodeOwners(Element eParent, Group group) throws ServiceException {
/* 218 */     Element eOwners = null;
/*     */     
/* 220 */     List<Group.GroupOwner> owners = Group.GroupOwner.getOwners(group, true);
/* 221 */     if (!owners.isEmpty()) {
/* 222 */       eOwners = eParent.addElement("owners");
/*     */       
/* 224 */       for (Group.GroupOwner owner : owners) {
/* 225 */         Element eOwner = eOwners.addElement("owner");
/*     */         
/* 227 */         eOwner.addAttribute("type", owner.getType().getCode());
/* 228 */         eOwner.addAttribute("id", owner.getId());
/* 229 */         eOwner.addAttribute("name", owner.getName());
/*     */       }
/*     */     }
/*     */     
/* 233 */     return eOwners;
/*     */   }
/*     */   
/*     */   public void docRights(List<AdminRight> relatedRights, List<String> notes)
/*     */   {
/* 238 */     relatedRights.add(Rights.Admin.R_getDistributionList);
/* 239 */     relatedRights.add(Rights.Admin.R_getGroup);
/* 240 */     notes.add(String.format("Attributes that are not allowed to be get by the authenticated admin will be returned as <a n=\"{attr-name}\" pd=\"1\"/>.To allow an admin to get all attributes, grant the %s right", new Object[] { Rights.Admin.R_getDistributionList.getName() }));
/* 241 */     notes.add(String.format("Attributes that are not allowed to be get by the authenticated admin will be returned as <a n=\"{attr-name}\" pd=\"1\"/>.To allow an admin to get all attributes, grant the %s right", new Object[] { Rights.Admin.R_getGroup.getName() }));
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/admin/GetDistributionList.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */